package com.hbnu.mysql;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 需求：处理查询结果集
 *
 * @author kittychen
 * @since 2025-10-20 19:40
 */
public class JdbcDemo03 {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 1、获取数据库配置文件中的数据库信息
            InputStream resourceAsStream = JdbcDemo03.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");

            // 2、注册驱动
            Class.forName(driver);

            // 3、获取数据库连接
            connection = DriverManager.getConnection(url, username, password);

            // 4、获取数据库操作对象
            statement = connection.createStatement();

            // 5、执行SQL语句
            String sql = "select id, username as name, password, salary from tb_user";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                // 5.1、字段的数据全部以字符串接收，通过索引
                // String id = resultSet.getString(1);
                // String name = resultSet.getString(2);
                // String pwd = resultSet.getString(3);
                // String salary = resultSet.getString(4);

                // 5.2、字段的数据以具体类型接收，通过索引
                // int id = resultSet.getInt(1);
                // String name = resultSet.getString(2);
                // String pwd = resultSet.getString(3);
                // double salary = resultSet.getDouble(4);

                // 5.3、字段的数据全部以字符串接收，通过字段名
                // String id = resultSet.getString("id");
                // String name = resultSet.getString("username");
                // String pwd = resultSet.getString("password");
                // String salary = resultSet.getString("salary");

                // 5.4、字段的数据以具体类型接收，通过字段名
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");  // 使用字段重命名后的名字
                String pwd = resultSet.getString("password");
                double salary = resultSet.getDouble("salary");

                System.out.println(id + "\t" + name + "\t" + pwd + "\t" + salary);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 6、关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
